package com.service.Impl;


import com.mapper.AccountMapper;
import com.mapper.TranRecordMapper;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pojo.Account;
import com.pojo.TranRecord;

/**
 * @author acoffee
 * @create 2021-11-16 16:49
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TranRecordMapper tranRecordMapper;

    @Override
    public void tranform(String fromAccountno, String toAccountno, float money) {
        Account toAccount = accountMapper.selectAccountByAccountno(toAccountno);
        Account fromAccount = accountMapper.selectAccountByAccountno(fromAccountno);

        //在自己的账户上转出指定金额
        accountMapper.updateBalanceByAccount(fromAccountno,fromAccount.getBalance() - money);
        //在别人的账户上加上指定金额
        accountMapper.updateBalanceByAccount(toAccountno,toAccount.getBalance()+money);

        //转出者的动账记录
        TranRecord fromTranRecord = new TranRecord();
        fromTranRecord.setType(-1);
        fromTranRecord.setMoney(money);
        fromTranRecord.setMessage(fromAccountno+"转出"+money+"元");
        tranRecordMapper.addRecord(fromTranRecord);

        //转入这的动账记录
        TranRecord toTranRecord = new TranRecord();
        toTranRecord.setType(1);
        toTranRecord.setMoney(money);
        toTranRecord.setMessage(toAccountno+"转入"+money+"元");
        tranRecordMapper.addRecord(toTranRecord);

    }

    @Override
    public void addAcount(Account account) {
        this.accountMapper.addAcount(account);
    }

    @Override
    public void updateAccount(Account account) {
        this.accountMapper.updateAccount(account);
    }


}
